<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Article;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;
use Illuminate\Http\RedirectResponse;

class IndexController extends Controller
{
    public function index(){
        return view('index');
    }
    //文章列表页，显示
    public function article_list(){
        $res = DB::table('articles')->get();
        $num=count($res);
        return view('article_list',['res'=>$res,'num'=>$num]);
    }

    //添加文章
    public function article_add(Request $req){
        date_default_timezone_set('PRC');
        if(!$req->session()->has('content')){
            if(!$_POST){
                return view('article_add');
            }else{
                if(!empty($_POST['submit'])){
                    $content=array();
                    $content['title'] = $req->title;
                    $content['detail'] = $req->detail;
                    $content['inclass'] = $req->inclass;
                    $content['content'] = $req->content;
                    $content['created_at']=date('Y-m-d H:i:s');
                    $res = DB::table('articles')->insert($content);
                    return redirect('index/article_list');
                }else{
                    $content=array();
                    $content['title'] = $req->title;
                    $content['detail'] = $req->detail;
                    $content['inclass'] = $req->inclass;
                    $content['content'] = $req->content;
                    $content['created_at']=date('Y-m-d H:i:s');
                    //先判断一下有没有缓存，为防止没清除缓存
                    if($req->session()->has('content')){
                        $req->session()->forget('content');
                    }
                    $req->session()->put('content',$content);
                        return redirect('index/article_list');
                }

            }
        }else{
            //取出缓存   并将它删除
            $value = $req->session()->pull('content','default');
            return view('article_add');

        }

    }





    public function article_update(Request $req,$aid){
        if(!$_POST){
            $art =  Article::find($aid);
            return view('article_update',['art'=>$art]);
        }else{
           $art =  Article::find($aid);
           $art->title = $req->title;
           $art->detail = $req->detail;
           $art->inclass = $req->inclass;
           $art->content = $req->content;
           $art->updated_at = date('Y-m-d H:i:s');
           if($art->save()){
              return redirect('index/article_list');
           }else{
              return false;
           }
         }
    }

    //单一删除
    public function article_del($aid){
            $res =  DB::table('articles')->where('id',$aid)->delete();
        if($res){
            return redirect('index/article_list');
        }
    }
    //批量删除
    public function article_dels($id){
            if(strpos($id,',') == true){
                $arr = explode(',',$id);
                foreach($arr as $k => $v){
                    DB::table('articles')->where('id','=',$v)->delete();
                }
            }else{
                DB::table('articles')->where('id',$id)->delete();
            }
    }
    public function article_ios(Request $req){
        if(!$_POST){
            return view('article_add');
        }else{
            $content = array();
            $content['title'] = $req->title;
            $content['detail'] = $req->detail;
            $content['inclass'] = $req->inclass;
            $content['content'] = $req->content;
            $content['create_at'] = date('Y-m-d H:i:s');

            Cache::forever('content',$content);
           // return Cache::get('content');

            return redirect('index/article_list');

        }


    }
}
